package cn.iotnova.rtc;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.view.Surface;

import androidx.annotation.NonNull;

import com.zxj.camerapreview.NativeSfu2Activity;
import com.zxj.camerapreview.NativeSfuActivity;

import java.util.HashMap;
import java.util.concurrent.locks.Lock;

public class Rtcclient {
    static {
        //System.loadLibrary("pcmopus");
        System.loadLibrary("rtcws");
    }

    private String sfuId;
    private String peerId;

    //ClientSignalInit
    public void setId(int typ, String id) {
        if (typ == 0) peerId = id;
        else if (typ == 1) sfuId = id;

        if (sfuActivity instanceof NativeSfuActivity)
            ((NativeSfuActivity)sfuActivity).startRecord();
        if (sfuActivity instanceof NativeSfu2Activity)
            ((NativeSfu2Activity)sfuActivity).startRecord();
    }

//    public void setSfuActivity(NativeSfuActivity activity) {
//        sfuActivity = activity;
//    }

    public void setSfuActivity(Activity activity) {
        sfuActivity = activity;
    }

    //sfu

    public native void ConnectToServerByCfg(int mediaType, int bufferSize, int _sampleRate, int _clockRate, int _channel, String host);
    public native void ConnectToServer(int mediaType, int bufferSize, String host);
    private native void SendNV21Image(String id, byte[] data, int width, int height, int size);
    public void SendNV21Image(byte[] data, int width, int height, int size) {
        if (sfuId == null) return;
        SendNV21Image(sfuId, data, width, height, size);
    }
    public native void SendNVRGB565Image(byte[] data, int width, int height);
    public native void SendYUVImage(byte[] data, int width, int height);
    private native void SendPCMData(String id, byte[] data, int length);
    public void SendPCMData(byte[] data, int length) {
        SendPCMData(sfuId, data, length);
    }
    private native void SendOpusData(String id, byte[] data, int length);
    public void SendOpusData(byte[] data, int length) {
        if (sfuId == null) return;
        SendOpusData(sfuId, data, length);
    }
    private native void SendOpusData2(String id, byte[] data, int length);
    public void SendOpusData2(byte[] data, int length) {
        SendOpusData(peerId, data, length);
    }
    public native void Init(Surface surface, AssetManager assetManager);
    //public native void Draw(String path);
    private native void ShowYuv(String id, byte[] dat, int len);
    public void ShowYuv(byte[] dat, int len) {
        if (sfuId == null) return;
        ShowYuv(sfuId, dat, len);
    }
    public native void Destroyed();
    public native void ClientSignalPeerInit(int mediaType, String host, String username, String passwd, String serial);
    //public static native void ClientSignalPeerInit(String hostname, String hostport, String hosturi, String username, String passwd, String serial);

    private native void SendNV21Image2(String id, byte[] data, int width, int height, int size);
    public void SendNV21Image2(byte[] data, int width, int height, int size) {
        if (peerId == null) return;
        SendNV21Image2(peerId, data, width, height, size);
    }
    public native void SendRGBAImage2(String id, byte[] data, int width, int height, int size);
    public void SendRGBAImage2(byte[] data, int width, int height, int size) {
        if (peerId == null) return;
        SendRGBAImage2(peerId, data, width, height, size);
    }



    ///////////////////////////////////////////////////////////////////////////////////////////

    public void Init(Context context) {
        activity = context;
        new Thread(new Runnable() {
            @Override
            public void run() {
                String conn_id;
                while (true) {
                    conn_id = ClientSignalInit("192.168.1.2", "7000", "clientws", "zyh", "123", "1");
                    //conn_id = ClientSignalInit("114.117.194.15", "7000", "clientws", "zyh", "123", "1");
                    //Websocket.setId(connId);
                    connId = conn_id;

                    ClientReadData(conn_id);
                }
            }
        }).start();
    }

    //NativeSfuActivity sfuActivity;
    Activity sfuActivity;

    Context activity;

    public ISignalcallback signalcallback = null;

    public void _Init(Context context) {
        activity = context;
        new Thread(new Runnable() {
            @Override
            public void run() {
                String connId;
                while (true) {
                    //connId = ClientSignalInit("114.117.194.15", "7000", "clientws", "zyh", "123", "124");
                    connId = ClientSignalInit("192.168.1.2", "7000", "clientws", "zyh", "123", "124");
                    //Websocket.setId(connId);
                    //id = connId;

                    ClientReadData(connId);
                    callbackMap.remove(connId);
                }
            }
        }).start();
    }

    public void Remove(String id) {
        callbackMap.remove(id);
    }

    public HashMap<String, ISignalcallback> callbackMap = new HashMap<String, ISignalcallback>();


    //private String id;

    private String connId;

    public void setId(String _id) {
        connId = _id;
    }

    public void setCallback(String id, ISignalcallback signalcallback) {
        callbackMap.put(id, signalcallback);
    }

    ///////////////////////////////////////////////////////////////////////////////////
    public void SocketRecv(String id, int type, String dat) {
        //activity.handleMessage(dat);
        //signalcallback.SignalRecv("", dat);
        callbackMap.get(id).SignalRecv(type, dat);
    }
    public native String ClientSignalInit(String hostname, String hostport, String hosturi, String username, String passwd, String serial);
    public native void ClientReadData(String id);
    public native void ClientSendData(String id, int type, String dat);
    private native void ClientClose(String id);
    public void Close(String id) {
        lock.lock();
        ClientClose(id);
        Remove(id);
        lock.unlock();
    }
    public Lock lock;





    //

    //public static final native long createEncoder(int sampleRateInHz, int channelConfig, int complexity);
    public final native int goencode(String id, @NonNull short[] lin, int sampleRate, int sampleSize, int level);
    public int goencode(@NonNull short[] lin, int sampleRate, int sampleSize, int level) {
        //return goencode(id, lin, sampleRate, sampleSize, level);
        if (sfuId == null) return 0;
        return goencode(sfuId, lin, sampleRate, sampleSize, level);
    }
    public final native int godecode(String id, @NonNull byte[] encoded, @NonNull short[] lin);
    //public static final native void destroyEncoder(long handle);
    //public static final native void destroyDecoder(long handle);
}
